﻿using System.Collections.Generic;
using System.Linq;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using WFramework.MvcPager;
using WFramework.ORM;

namespace MiniWeb.Dao
{
    /// <summary>
    /// 
    /// </summary>
    public class AdminUserDao : BaseDao<AdminUser>
    {
        public void Delete(int id)
        {
            var sql = Sql.Builder.Where("AdminUserId = @0", id);
            Delete(sql);
        }

        public AdminUser GetById(int id)
        {
            var sql = Sql.Builder.Where("AdminUserId = @0", id);
            return FirstOrDefault(sql);
        }

        public AdminUser GetByUserName(string userName)
        {
            var sql = Sql.Builder.Where("UserName = @0", userName);
            return FirstOrDefault(sql);
        }

        public PagedList<AdminUser> GetPagedList(string account, int pageIndex, int pageSize)
        {
            var sql = Sql.Builder.Where("1=1");
            if (!string.IsNullOrEmpty(account))
                sql.Where("au.UserName LIKE @0", string.Format("%{0}%", account));


            return PagedList<AdminUser>(pageIndex, pageSize, sql);
        }

        public bool Exits(string userName)
        {
            var sql = Sql.Builder.Where("UserName  = @0", userName);
            if(FirstOrDefault(sql)!=null)
            {
                return true;
            }
            return false;
        }

        public List<AdminUser> GetListByNoAdmin()
        {
            var sql = Sql.Builder.Where("RoleId != 1");
         
            return Query(sql).ToList();
        }
    }
}

